package com.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Scanner;

/**
 * @author lenovo
 * @Description
 * @Date2025-2-25 15:14
 */
public class JDBCDemo {

    public static void showStudents(){
        List<Student> list=JDBCUtils.query(Student.class,"select * from student_s");
        for(Student s : list){
            System.out.println(s);
        }
    }
//        try(Connection conn=DateSourceUtils.getConnection()){
//            PreparedStatement ps=conn.prepareStatement("select * from student_s ");
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()){
//                int id = rs.getInt("id");
//                String name=rs.getString("name");
//                int age = rs.getInt("age");
//                System.out.println("id:"+id+"name:"+name+"age:"+age);
//
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

    //实现学生搜索，输入字符，只要名字输入字符的都可以显示出来
    public static void selectStudent(String sname){
        List<Student> list = JDBCUtils.query(Student.class,
                "select * from student_s where name like ?",
                "%"+sname +"%");
        for (Student s :list){
            System.out.println(s);
        }
    }
//        try(Connection conn=DateSourceUtils.getConnection()){
//            PreparedStatement ps=conn.prepareStatement("select * from student_s where name like ?");
//            ps.setString(1,"%"+sname+"%");
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()){
//                int id = rs.getInt("id");
//                String name=rs.getString("name");
//                int age = rs.getInt("age");
//                System.out.println("id:"+id+"name:"+name+"age:"+age);
//
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }


    public static boolean login(String username,String password){
        List<User> list = JDBCUtils.query(User.class,
                "select * from t_user where username = ? and password = ?",
                username,password);
        if(list.size() > 0){
            return true;
        }
        return false;
    }


    public static void main(String[] args){
//        showStudents();
        selectStudent("三");
//        JDBCUtils.update("insert into student(name,age,gender,cellphone,address) values(?,?,?,?,?)",
////                "赵小六",18,"女","15345678111","北京");
        if(login("zhangsan","123456")){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }

    }
}
